﻿using Dark_Core_Server.Models.Message;
using Dark_Core_Server.Models.Universal;
using Dark_Core_Server.Models.User;
using Dark_Core_Server.Service;
using DarkSKL_Core.ArgModules;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MongoDB.Bson;

namespace Dark_Core_Server.Controllers
{
    [Route("CoreService/[controller]:[action]")]
    [ApiController]
    public class MessageController : ControllerBase
    {
        private readonly MessageService MessageService;
        private readonly UserService UserService;

        public MessageController(MessageService o1,UserService o2)
        {
            this.MessageService = o1;
            this.UserService = o2;
        }

        [HttpPost]
        [Authorize]
        public async Task<ActionResult> Publish(SimpleMsgRecord record)
        {
            ResponseMsg<UserDocument> userResponseMsg = await UserService.ParseHttpRequestToUser(HttpContext.Request);
            if (userResponseMsg.Status != UniversalState.Success)
                return Ok(false.CreateResponseMsg("User not found", UniversalState.Error));
            return Ok((await MessageService.CreateMsg(userResponseMsg.Data.id,record)));
        }

        [HttpPost]
        [Authorize]
        public async Task<ActionResult> Subscribe(PostRequestArgs args)
        {
            ResponseMsg<UserDocument> userResponseMsg = await UserService.ParseHttpRequestToUser(HttpContext.Request);
            if (userResponseMsg.Status != UniversalState.Success)
                return Ok(false.CreateResponseMsg("User not found", UniversalState.Error));
            args.args.TryGetValue("payload", out var payload);
            if (payload == null) return Ok("No payload found in request.".CreateResponseMsg("缺少负载payload", UniversalState.Error));
            args.args.TryGetValue("pageNum", out var pageNum);
            args.args.TryGetValue("pageSize", out var pageSize);
            return Ok((await MessageService.QueryMsg(args.mode.ToMsgQueryMode(), userResponseMsg.Data,payload.ToString(),Convert.ToInt32(pageNum),Convert.ToInt32(pageSize))));
        }

        [HttpPost]
        [Authorize]
        public async Task<ActionResult> UnPublish(PostRequestArgs args)
        {
            ResponseMsg<UserDocument> userResponseMsg = await UserService.ParseHttpRequestToUser(HttpContext.Request);
            if (userResponseMsg.Status != UniversalState.Success)
                return Ok(false.CreateResponseMsg("User not found", UniversalState.Error));
            args.args.TryGetValue("payload", out var payload);
            if (payload == null) return Ok("No payload found in request.".CreateResponseMsg("缺少负载payload", UniversalState.Error));
            if (!ObjectId.TryParse(payload.ToString(), out var msgId))
                return Ok(false.CreateResponseMsg("Invalid message id", UniversalState.Error));

            return Ok((await MessageService.DeleteMsg(userResponseMsg.Data.id, msgId)));
        }


    }
}
